package com.weigee.ktv.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import com.linde.library.activities.MyFragmentActivity
import com.linde.library.eventbus.EventSendEntity
import com.linde.library.util.DecimalPointUtil
import com.linde.library.util.DisplayUtil
import com.linde.library.util.ListUtil
import com.weigee.ktv.R
import com.weigee.ktv.model.EventTag
import com.weigee.ktv.presenter.SupermarketShoppingListPresenter
import com.weigee.ktv.view.dialog.LoadingDialog
import com.weigee.ktv.view.interfaces.SupermarketShoppingListView
import kotlinx.android.synthetic.main.activity_supermarket_shopping_list.*
import kotlinx.android.synthetic.main.include_status_bar_height.*
import kotlinx.android.synthetic.main.include_title_bar.*
import org.simple.eventbus.Subscriber

/**
 * Created by linde on 2017/6/13.
 * LinDe
 * 超市商品列表
 */
class SupermarketShoppingListFragment : BaseFragment(), SupermarketShoppingListView
{
    private var presenter: SupermarketShoppingListPresenter? = null
    private var dialog: LoadingDialog? = null
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        presenter = SupermarketShoppingListPresenter(this)
        presenter?.onCreate(savedInstanceState)
        presenter?.onCheckArguments(arguments)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        return inflater?.inflate(R.layout.activity_supermarket_shopping_list, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
        // 状态栏高度
        DisplayUtil.setStatusBarHeight(linear_status_bar_height)
        // 设置返回键处理逻辑
        (activity as MyFragmentActivity).onBackPressedListener = object : MyFragmentActivity.OnBackPressedListener
        {
            override fun onBackPressed(): Boolean
            {
                if (frame_dialog_shopping_cart.visibility == View.VISIBLE)
                {
                    setShoppingCartDetailVisibility(false)
                    return true
                }
                return false
            }
        }
        // 隐藏购物车详情
        frame_dialog_shopping_cart.visibility = View.GONE
        // 返回／退出
        my_title_bar?.leftIconClickBack = activity
        // 标题
        my_title_bar?.title = "超市"

        ListUtil.initRecyclerView(rv_shopping_category, presenter?.categoryAdapter)
        ListUtil.initRecyclerView(rv_shopping_list, presenter?.listAdapter)
        ListUtil.initRecyclerView(rv_shopping_cart, presenter?.cartAdapter)

        // 显示／隐藏购物车列表
        linear_shopping_cart_group.setOnClickListener {
            setShoppingCartDetailVisibility(frame_dialog_shopping_cart.visibility == View.GONE)
        }
        // 空事件，防止点击title误关
        frame_dialog_top.setOnClickListener {
            // do nothing
        }
        // 点击dialog关闭之
        frame_dialog_shopping_cart.setOnClickListener {
            setShoppingCartDetailVisibility(false)
        }
        // 清空购物车
        tv_clear_shopping_cart.setOnClickListener {
            presenter?.clearShoppingCart()
            setShoppingCartDetailVisibility(false)
        }
    }

    // 购物车详情列表的显示/隐藏
    private fun setShoppingCartDetailVisibility(isVisible: Boolean)
    {
        val isDialogVisible = frame_dialog_shopping_cart.visibility == View.VISIBLE
        if (isVisible && !isDialogVisible && presenter?.isCanShowShoppingCartDialog() ?: false)
        {
            frame_dialog_shopping_cart.visibility = View.VISIBLE
            frame_dialog_shopping_cart.startAnimation(AnimationUtils.loadAnimation(activity, com.linde.library.R.anim.dialog_bottom_to_top_enter))
        }
        else if (!isVisible && isDialogVisible)
        {
            frame_dialog_shopping_cart.visibility = View.GONE
            frame_dialog_shopping_cart.startAnimation(AnimationUtils.loadAnimation(activity, com.linde.library.R.anim.dialog_bottom_to_top_exit))
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?)
    {
        super.onActivityCreated(savedInstanceState)
        presenter?.onActivityCreated(savedInstanceState)
    }

    override fun onResume()
    {
        super.onResume()
        presenter?.onResume()
    }

    override fun taskPreview()
    {
        dialog?.dismiss()
        dialog = LoadingDialog()
        dialog?.show(childFragmentManager, javaClass.name)
    }

    override fun taskCompletely()
    {
        dialog?.dismiss()
        dialog = null
    }

    override fun onLoadShoppingCategoryFailure(throwable: Throwable?)
    {
        throwable?.printStackTrace()
    }

    override fun onLoadShoppingCategoryErrorData()
    {
    }

    override fun onLoadShoppingListFailure()
    {
    }

    override fun onLoadShoppingListErrorData()
    {
    }

    override fun onDismissShoppingCartDetail()
    {
        setShoppingCartDetailVisibility(false)
    }

    override fun onUpdateShoppingCartNum(cartNum: Int, prise: Float?)
    {
        if (cartNum > 0)
        {
            frame_tip_num_all.visibility = View.VISIBLE
            tv_cart_num_all.text = cartNum.toString()
            tv_commit.isEnabled = true
            tv_commit.setOnClickListener({
                //                SnackUtil.show(v, "总价：" + prise)
                val arguments = Bundle()
                arguments.putFloat("prise", prise ?: 0F)
                MyFragmentActivity.start(activity, ApplySupermarketOrderFragment::class.java, arguments)
            })
        }
        else
        {
            frame_tip_num_all.visibility = View.GONE
            tv_commit.isEnabled = false
        }
        tv_prise_all.text = ("¥ " + DecimalPointUtil.point(2, prise))
    }

    @Subscriber(tag = EventTag.eventSelectedShoppingCategory)
    private fun onSelectedShoppingCategory(eventSendEntity: EventSendEntity?)
    {
        presenter?.onSelectedShoppingCategory(eventSendEntity)
    }
}